﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.IO;

namespace myTimesheet
{
    public class Projects
    {
        private string _DatabaseFile = "Data.Bin";

        public void Add(string Title) 
        {
            Guid guid = Guid.NewGuid();

            if (!isAdded(guid))
            {
                DataSet ds = GetProjects();

                DataRow r = ds.Tables[0].NewRow();
                r["GUID"] = guid;
                r["Title"] = Title;

                ds.Tables[0].Rows.Add(r);

                ds.WriteXml(_DatabaseFile, XmlWriteMode.IgnoreSchema);
            }
        }

        public DataSet GetProjects() 
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Projects");

            ds.Tables[0].Columns.Add("GUID");
            ds.Tables[0].Columns.Add("Title");
            ds.Tables[0].Columns.Add("Default");
            ds.Tables[0].Columns.Add("PaidDate");

            ds.Tables.Add("TimeSheet");
            ds.Tables[1].Columns.Add("GUID");
            ds.Tables[1].Columns.Add("ProjectGUID");
            ds.Tables[1].Columns.Add("Start");
            ds.Tables[1].Columns.Add("End");


            if (!File.Exists(_DatabaseFile))
                ds.WriteXml(_DatabaseFile, XmlWriteMode.IgnoreSchema);

            ds.ReadXml(_DatabaseFile, XmlReadMode.IgnoreSchema);

            return ds;
        }

        public DataSet GetProjectDetails(string ProjectGUID)
        {
            DataSet dsProject = GetProjects();

            DataSet ds = new DataSet();
            ds.Tables.Add();
            ds.Tables[0].Columns.Add("PaidDate");

            foreach (DataRow dr in dsProject.Tables[0].Rows)
            {
                if (dr["GUID"].ToString() == ProjectGUID)
                {
                    DataRow r = ds.Tables[0].NewRow();
                    r["PaidDate"] = dr["PaidDate"];

                    ds.Tables[0].Rows.Add(r);
                }
            }

            return ds;
        }

        public void SetProjectDetails(string ProjctGUID, DateTime PaidDate)
        {
            DataSet ds = GetProjects();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["Guid"].ToString() == ProjctGUID.ToString())
                    dr["PaidDate"] = PaidDate;
            }



            ds.WriteXml(_DatabaseFile, XmlWriteMode.IgnoreSchema);
        }

        public void SetDefault(string guid) 
        {
            DataSet ds = GetProjects();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dr["Default"] = 0;

                if (dr["GUID"].ToString() == guid)
                    dr["Default"] = 1;
            }

            ds.WriteXml(_DatabaseFile, XmlWriteMode.IgnoreSchema);
        }

        public string GetDefault() 
        {
            DataSet ds = GetProjects();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["Default"].ToString() == "1")
                    return dr["GUID"].ToString();
            }

            return "";
        }

        public Boolean isAdded(Guid guid)
        {
            DataSet ds = GetProjects();


            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["GUID"].ToString()==guid.ToString())
                    return true;
            }

            return false;
        }

        public string SetStart(string ProjctGUID) 
        {
            DataSet ds = GetProjects();

            DataRow dr = ds.Tables[1].NewRow();

            Guid guid = Guid.NewGuid();

            dr["GUID"] = guid;
            dr["ProjectGUID"] = ProjctGUID;
            dr["Start"] = DateTime.Now;
            dr["End"] = DateTime.Now;

            ds.Tables[1].Rows.Add(dr);

            ds.WriteXml(_DatabaseFile, XmlWriteMode.IgnoreSchema);

            return guid.ToString();
        }

        public void SetEnd(string GUID)
        {
            DataSet ds = GetProjects();

            foreach (DataRow dr in ds.Tables[1].Rows)
            {
                if (dr["Guid"].ToString() == GUID.ToString())
                    dr["End"] = DateTime.Now;
            }

            

            ds.WriteXml(_DatabaseFile, XmlWriteMode.IgnoreSchema);
        }

        public DataSet GetTime(string ProjectGUID)
        {
            DataSet dsProject = GetProjects();

            DataSet ds = new DataSet();
            ds.Tables.Add();
            ds.Tables[0].Columns.Add("Start");
            ds.Tables[0].Columns.Add("End");

            foreach (DataRow dr in dsProject.Tables[1].Rows)
            {
                if (dr["ProjectGUID"].ToString()==ProjectGUID)
                {
                    DataRow r = ds.Tables[0].NewRow();
                    r["Start"] = dr["Start"];
                    r["End"] = dr["End"];

                    ds.Tables[0].Rows.Add(r);
                }
            }

            return ds;
        }
    }
}
